﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LunwenYun.Infrastructure.Entities.Entity;
using LunwenYun.Portal.Biz;
using LunwenYun.Portal.WebLogic;
using LunwenYun.Tool.Framework;

namespace LunwenYun.Portal.Web.Controllers
{
    public class jController : SiteBaseController
    {
        //
        // GET: /j/

        public ActionResult index(int id,int page=0)
        {
            var periodical = new Periodical().Find(id.ToString());
            if (periodical == null)
            {
                return RedirectToAction("noperiodical");
            }
            ViewBag.Periodical = periodical;

            var purpose = SystemBiz.GetAllPurpose(id, page, 10);
            ViewBag.Purpose = new ItemListBox<Purpose>(purpose).BuildPage(
                SystemBiz.GetAllPurposeForPage(id),page,10,
                new PageParameter() { Style = "home" }); ;

            var agent = SystemBiz.GetAllPeriodicalAgent(id, 0, 15);
            ViewBag.Agent = agent;
            return View();
        }
        //public ActionResult jList(int ind = -1, int apt = -1, int lang = -1,string journalName="", int page = 0)
        //{
        //    ViewBag.IndType = ind;
        //    ViewBag.Aptitude = apt;
        //    ViewBag.Language = lang;
        //    ViewBag.JournalName = journalName;
        //    ViewBag.Data = new ItemListBox<Periodical>(SystemBiz.GetAllPeriodical(ind, -1, apt, lang,journalName, page, 15))
        //        .BuildPage(SystemBiz.GetAllPeriodicalForPage(ind, -1, apt, lang,journalName), page, 15,
        //        new PageParameter() { Style = "home" });
        //    return View();
        //}
        public ActionResult mList(int ind = -1, int apt = -1, int lang = -1, int page = 0)
        {
            ViewBag.IndType = ind;
            ViewBag.Aptitude = apt;
            ViewBag.Language = lang;
            List<Article> list = SystemBiz.GetAllManuscript(ind, -1, apt, lang, page, 8);
            ViewBag.Data = new ItemListBox<Article>(list).BuildPage(
                SystemBiz.GetAllManuscriptForPage(ind, -1, apt, lang), page, 8,
                new PageParameter()
                {
                    Style = "Home"
                });
            return View();
        }
        public ActionResult industry() 
        {
            return View();
        }
        /// <summary>
        /// 石油天然气工业，行业投稿
        /// </summary>
        /// <returns></returns>
        public ActionResult oilandgas()
        {
            var cs = SystemBiz.GetIndustryCS(1,true, 0, 15);
            ViewBag.Cs = cs;
            return View();
        }

        public ActionResult industrycontribution()
        {
            return View();
        }

        public ActionResult NoPeriodical()
        {
            return View();
        }
        public ActionResult article(int id, int page = 0)
        {
            Article article = new Article().Find(id.ToString());
            if (article == null)
            {
                return Redirect("/");
            }
            ViewBag.Article = article;

            var messageboard = new ItemListBox<MessageBoard>(SystemBiz.GetAllAgentMessage(id, 0, 8))
                .BuildPage(SystemBiz.GetAllAgentMessageForPage(id), page, 8, new PageParameter() { Style = "home" });
            ViewBag.MessageBoard = messageboard;
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult postmessage(int id, string message)
        {
            if (HttpContext.User.Identity.IsAuthenticated)
            {
                var user = new User().Find(HttpContext.User.Identity.Name);
                if (user != null && user.SystemRole == Infrastructure.Entities.Enum.UserRole.PaperAgent)
                {
                    SystemBiz.NewMessageForArticle(id, HttpContext.User.Identity.Name, message.Replace("\r\n","<br />").NoHTMLLabel("script"));
                }
            }
            return RedirectToAction("article",new {id = id});
        }
        public int deletemessage(int id)
        {
            if (HttpContext.User.Identity.IsAuthenticated)
            {
                var username = HttpContext.User.Identity.Name;
                var user = new User().Find(username);
                if (user != null && user.SystemRole == Infrastructure.Entities.Enum.UserRole.Admin)
                {
                    var message = new MessageBoard().Find(id.ToString());
                    if (message != null)
                    {
                        message.PostDelete();
                        return 1;
                    }
                }
            }
            return 0;
        }
       
        
    }
}
